<?php

namespace backend\modules\rbac\controllers;

use backend\modules\base\controllers\BaseController;
use backend\modules\rbac\models\AuthItem;
use backend\modules\rbac\models\AuthItemChild;
use backend\modules\rbac\models\MenuPerm;
use common\lib\Singleton;
use Yii;
use yii\base\Exception;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * ItemController implements the CRUD actions for Item model.
 */
class ItemController extends BaseController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login', 'error'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['index','create','view','update','addItem','permtorole','getmenuperm','indexitem','setindexitem'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Item models.
     * @return mixed
     */
    public function actionIndex()
    {
        $menuPerm = new MenuPerm();
        $roles = $menuPerm->getRoles();
        return $this->render('index',['roles'=>$roles]);
    }

    public function actionGetmenuperm()
    {
        $post = Yii::$app->request->post();
        $name = isset($post['name'])?$post['name']:'';
            $menuPerm = new MenuPerm();
            $menuPerm = $menuPerm->getLeftMenu($name);
            return json_encode($menuPerm);

    }

    /**
     * 配置角色菜单权限
     *
     * @return string
     * @throws \Pheanstalk\Exception
     * @throws \yii\db\Exception
     */
    public function actionPermtorole()
    {
        $post = Yii::$app->request->post();
        $roleName = $post['roleName'];
        $menuId = $post['menuId'];
        $menuPerm = new MenuPerm();
        $beginTransaction = Yii::$app->db->beginTransaction();
        try{
            $menuPerm->setRoleBymenuId($menuId,$roleName);
            $beginTransaction->commit();
        }catch (Exception $e){
            $beginTransaction->rollBack();
            return $e->getMessage();
        }
        return json_encode(['code'=>200]);
    }

    /**
     * 根据角色获取首页功能模块权限
     * @return string
     */
    public function actionIndexitem()
    {
        $post = Yii::$app->request->post();
        if(isset($post['name'])) {
            $itemARR = array();
            $childARR = AuthItemChild::find()->select(['child'])->where(['parent'=>$post['name']])->asArray()->all();
            foreach($childARR as $k=>$v) {
                if(substr($v['child'],0,5)=='Index')
                {
                    $itemARR[] = $v['child'];
                }
            }

            $nameARR = array();
            $AuthItemARR = AuthItem::find()->select(['name'])->andFilterWhere(['like', 'name', 'Index@'])->asArray()->all();
            foreach($AuthItemARR as $k=>$v) {
                if(in_array($v['name'],$itemARR)){
                    $nameARR[] = ['checked'=>1,'item_name'=>$v['name']];
                } else{
                    $nameARR[] = ['checked'=>0,'item_name'=>$v['name']];
                }
            }
        }
        return json_encode(['code'=>200,'data'=>$nameARR]);
    }

    /**
     * 设置首页模块
     * @return string
     */
    public  function actionSetindexitem()
    {
        $post = Yii::$app->request->post();
        $checkValue = isset($post['checkValue'])?$post['checkValue']:[];
        $checkValue = array_unique($checkValue);
        try{
            foreach($checkValue as $k=>$v) {
                $AuthItemChild = new AuthItemChild;
                $AuthItemChild->parent = $post['roleName'];
                $AuthItemChild->child = $v;
                if(!$AuthItemChild->save()){
                    throw new Exception('保存失败');
                }
            }
        }catch (Exception $e){
            return json_encode(['code'=>500]);
        }
        return json_encode(['code'=>200]);
    }
}
